<template>
	<el-popover
		v-model="showPopover"
		placement="bottom"
		trigger="click"
		class="width-100-percent"
		:title="`${isAdd ? '新增' : '修改'}岗位`"
		@hide="onHideAddPostPopover"
	>
		<el-select v-model="selectPostId" placeholder="岗位">
			<el-option
				v-for="item in postList"
				:key="item.id"
				:label="item.name"
				:value="item.id"
				:disabled="dateTimeItem.scheduleEmpPostList ? dateTimeItem.scheduleEmpPostList.filter(x => x.postId === item.id).length > 0 : false"
			></el-option>
		</el-select>
		<div slot="reference">
			<slot name="reference"></slot>
		</div>
		<div class="margin-top-10 flex-start-center width-100-percent">
			<el-button type="danger" class="width-100-percent" @click="showPopover = false">取消</el-button>
			<el-button type="primary" class="width-100-percent" @click="onConfirmBtn">确定</el-button>
		</div>
	</el-popover>
</template>
<script>
    import {
        workforceManagementScheduleEmpPostCreate,
        workforceManagementScheduleEmpPostUpdate
    } from '../../../../../../api/schedule/scheduling';

    export default {
        props: {
            readonly: {
                type: Boolean,
                default: false
            },
            isAdd: {
                type: Boolean,
                default: false
            },
            checkData: {
                type: Object,
                default() {
                    return {};
                }
            },
            empItem: {
                type: Object,
                default() {
                    return {};
                }
            },
            dateTimeItem: {
                type: Object,
                default() {
                    return {};
                }
            },
            postItem: {
                type: Object,
                default() {
                    return {};
                }
            },
            postList: {
                type: Array,
                default() {
                    return [];
                }
            }
        },
        data() {
            return {
                showPopover: false,
                selectPostId: null
            };
        },
        methods: {
            onConfirmBtn() {
                // 新增岗位
                if (this.isAdd) {
                    const item = this.postList.find(x => x.id === this.selectPostId) || {};
                    workforceManagementScheduleEmpPostCreate({
                        postId: item.id,
                        postName: item.name,
                        scheduleEmpId: this.empItem.id,
                        scheduleEmpPostDateId: this.dateTimeItem.id
                    }).then(res => {
                        this.showPopover = false;
                        if (res.data.status === 200) {
                            this.$emit('on-refresh');
                        }
                    });
                } else {
                    // 修改岗位
                    const item = this.postList.find(x => x.id === this.selectPostId) || {};
                    workforceManagementScheduleEmpPostUpdate({
                        id: this.postItem.id,
                        postId: item.id,
                        postName: item.name,
                        scheduleEmpPostDateId: this.dateTimeItem.id
                    }).then(res => {
                        this.showPopover = false;
                        if (res.data.status === 200) {
                            this.$emit('on-refresh');
                        }
                    });
                }
            },
            onHideAddPostPopover() {
                this.selectPostId = null;
            }
        }
    };
</script>
